package com.zzhrtech.wgzx_cityzen.ui.task;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.GsonRequest;
import com.zzhrtech.WgzxApplication;
import com.zzhrtech.wgzx_cityzen.BaseActivity;
import com.zzhrtech.wgzx_cityzen.Constans;
import com.zzhrtech.wgzx_cityzen.R;
import com.zzhrtech.wgzx_cityzen.adapters.task.TaskFilterAdapter;
import com.zzhrtech.wgzx_cityzen.beans.task.TaskFilterBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TaskRizhiActivity extends BaseActivity {

    RecyclerView recy_rizhi;
    TaskFilterAdapter filterAdapter;
    String taskId;
    LinearLayout layout_content;

    String[] zcstatuss = new String[]{"已发布","待确认","已确认","已派修","已反馈"};
    String[] qxstatuss = new String[]{"已发布","待确认","已取消","已确认","已派修","已反馈"};
    String[] spstatuss = new String[]{"已发布","待确认","待审批","已审批","已确认","已派修","已反馈"};
    String[] statuss;
    String nowstatus;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_rizhi);

        toolbar.setNavigationIcon(R.mipmap.ic_close);

        taskId = getIntent().getStringExtra("taskid");
        nowstatus = getIntent().getStringExtra("now");
        initView();
//        httpGetRizhi(taskId);
    }

    private void initView(){

        layout_content = (LinearLayout) findViewById(R.id.layout_content);



        LayoutInflater inflater = LayoutInflater.from(this);

        if(nowstatus.equals("已取消")){
            statuss = qxstatuss;
        }else if (nowstatus.equals("待审批") || nowstatus.equals("已审批")){
            statuss = spstatuss;
        }else {
            statuss = zcstatuss;
        }


        int textColor = Color.parseColor("#000000");
        int pointColor = R.drawable.shape_point1;
        int lineColor = Color.parseColor("#89bc65");

        for (int i = 0;i < statuss.length;i++){
            View view = inflater.inflate(R.layout.item_taskstatus,null);
            TextView tv_status = (TextView) view.findViewById(R.id.tv_status);
            ImageView iv_status = (ImageView) view.findViewById(R.id.iv_status);
            View lineView = (View) view.findViewById(R.id.lineView);
            tv_status.setText(statuss[i]);
            tv_status.setTextColor(textColor);
            iv_status.setBackgroundResource(pointColor);
            lineView.setBackgroundColor(lineColor);
            if (nowstatus.equals(statuss[i])){
                iv_status.setBackgroundResource(R.drawable.shape_point);
                tv_status.setTextColor(Color.parseColor("#599e2e"));
                pointColor = R.drawable.shape_point2;

                textColor = getResources().getColor(R.color.black999);
                lineColor = getResources().getColor(R.color.black999);
                if (i != statuss.length - 1){
                    lineView.setBackgroundColor(textColor);
                }
            }

            if (i == statuss.length - 1){
                RelativeLayout pointLayout = (RelativeLayout) view.findViewById(R.id.pointLayout);
                pointLayout.removeView(lineView);
            }

            layout_content.addView(view);
        }


    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {

            List<TaskFilterBean.Data> dataList = (List<TaskFilterBean.Data>) msg.obj;
            for (TaskFilterBean.Data data : dataList){
//                Log.d("name=========]",data.getOneImgTh());
                filterAdapter.addItem(data);
            }

            return false;
        }
    });


    public void httpGetRizhi(String taskid){

        Map<String,String> params = new HashMap<String, String>();
        params.put("task_id",taskid);


        GsonRequest<TaskFilterBean> gsonRequest = new GsonRequest<TaskFilterBean>(com.android.volley.Request.Method.POST, Constans.HTTP_TASKLOG, TaskFilterBean.class, params, new com.android.volley.Response.Listener<TaskFilterBean>() {
            @Override
            public void onResponse(TaskFilterBean response) {

                if (response.isSuccess()){
                    Message message = new Message();
                    message.what = 0;
                    message.obj = response.getData();
                    mHandler.sendMessage(message);
                }else {


                    Log.d("fail========",response.getMsg());
//                    showToast(tv_time,response.getMsg());
                }
            }
        }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("result=====",error + "");
            }
        });

        WgzxApplication.addRequest(gsonRequest, Constans.reqTag);
    }

}
